﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using RainEngine.Controllers;
using RainEngine.SceneManagement;

namespace RainEngine.SceneNodes.UI
{
    /// <summary>
    /// 竖直滚动条
    /// </summary>
    public class UIVScrollBar : UIScrollBarBase
    {
        //默认值        
        private static readonly Rectangle DefaultButtonRectOrigin = new Rectangle(0, 16, 16, 16);
        private static readonly Rectangle DefaultButtonRectHighlight = new Rectangle(16, 16, 16, 16);
        private static readonly Rectangle DefaultButtonRectClicked = new Rectangle(32, 16, 16, 16);
        /// <summary>
        /// 默认竖直滚动条大小为16*232。
        /// </summary>
        private static readonly Vector2 DefaultVSize = new Vector2(16, 232);

        /// <summary>
        /// 创建一个默认竖直滚动条。
        /// </summary>
        /// <param name="engine">引擎</param>
        /// <param name="setScene">所属场景</param>
        public UIVScrollBar(RainGE engine, Scene setScene)
            : base(engine, setScene,DefaultVSize , Vector2.Zero, "UIScrollBar", DefaultButtonRectOrigin, DefaultButtonRectHighlight, DefaultButtonRectClicked, DefaultSliderRailRect, DefaultSliderButtonRectOrigin, DefaultSliderButtonRectHighlight, DefaultSliderButtonRectClicked, DefaultSliderButtonGlyphRect,true)
        { 
         
            //添加向上按钮
            btnLeftUp = new UIButton(engine, setScene, btnLeftUpPositon, TextureName, new Vector2(buttonWidth,buttonWidth ), null, engine.DefaultFont, Color.White, Color.White, buttonRectOrigin, buttonRectHighlight, buttonRectClicked,true);
            btnLeftUp.TabStop = false;
            btnLeftUp.MouseClick += new System.Windows.Forms.MouseEventHandler(btnUpLeft_MouseClick);
            Scene.AddNode(btnLeftUp);

            //添加向下按钮
            btnRightDown = new UIButton(engine, setScene, btnRightDownPosition, TextureName, new Vector2(buttonWidth, buttonWidth), null, engine.DefaultFont, Color.White, Color.White, buttonRectOrigin, buttonRectHighlight, buttonRectClicked,true);
            //将向下按钮垂直翻转
            btnRightDown.SpriteEffect = SpriteEffects.FlipVertically;
            btnRightDown.TabStop = false;
            btnRightDown.MouseClick += new System.Windows.Forms.MouseEventHandler(btnDownRight_MouseClick);
            Scene.AddNode(btnRightDown);
            
            Redraw();
        }

        public override void Update(GameTime gameTime)
        {

            //设置滑块位置，计算滚动条的值并引发ValueChanged事件           
            if (isMouseInside && Input.MouseLeftButtonPressed)
            {
                isScrolling = true;

                //根据鼠标移动改变滑块的竖直位置
                sliderButtonPosition.Y = Position.Y + buttonWidth;
                sliderButtonPosition.Y += Input.MousePos.Y - Position.Y - buttonWidth - sliderButtonSizeX / 2;

                //不让滑块超出滚动条边界
                if (sliderButtonPosition.Y < Position.Y + buttonWidth)
                    sliderButtonPosition.Y = Position.Y + buttonWidth;
                else if (sliderButtonPosition.Y > Position.Y + buttonWidth + sliderRailSizeX - sliderButtonSizeX)
                    sliderButtonPosition.Y = Position.Y + buttonWidth + sliderRailSizeX - sliderButtonSizeX;

                int value = 0;
                value = (int)(System.Math.Round((Input.MousePos.Y - Position.Y - buttonWidth - sliderButtonSizeX / 2) * maxStep / (sliderRailSizeX - sliderButtonSizeX)));
                //不让value的值超出范围
                if (value < 0)
                    value = 0;
                else if (value > maxStep)
                    value = maxStep;

                //如果值变化则重新设置新值并引发ValueChanged事件
                if (this.value != value)
                {
                    this.value = value;
                    OnValueChanged();
                }

                else
                    isScrolling = false;
                base.Update(gameTime);
            }
        }

        /// <summary>
        /// 绘制滚动条
        /// </summary>
        public override int Draw(GameTime gameTime)
        {

            //获取图像淡入淡出的透明颜色
            alphaTextureColor = new Color(Color.R ,Color .G,Color .B, Scene.TransitionAlpha);

            //绘制滑动条轨道
            RainGE.SpriteBatch.Draw(texture, new Rectangle((int)(sliderRailPosition.X), (int)(sliderRailPosition.Y), (int)sliderRailSizeX, (int)buttonWidth), sliderRailRect, alphaTextureColor, MathHelper.PiOver2, Origin, SpriteEffect, LayerDepth);

            //根据鼠标移入还是按下设置滑动条按钮图像源矩阵
            Rectangle buttonRect = isMouseInside || (uiManager.ActiveControl == this) ? ((Input.MouseLeftButtonPressed && isMouseInside) ? sliderButtonRectClicked : sliderButtonRectHighlight) : sliderButtonRectOrigin;

            //绘制滑块
            RainGE.SpriteBatch.Draw(texture, new Rectangle((int)sliderButtonPosition.X, (int)sliderButtonPosition.Y, (int)sliderButtonSizeX, (int)buttonWidth), buttonRect, alphaTextureColor, MathHelper.PiOver2, Origin, SpriteEffect, LayerDepth);

            //绘制滑块上的摩擦条，如果滑块高度小于滚动条宽度则无需绘制摩擦条
            if (sliderButtonSizeX > size.X)
                RainGE.SpriteBatch.Draw(texture, new Rectangle((int)sliderButtonPosition.X, (int)(sliderButtonPosition.Y + sliderButtonSizeX / 2 - buttonWidth / 2), (int)buttonWidth, (int)buttonWidth), sliderButtonGlyphRect, Color, MathHelper.PiOver2, Origin, SpriteEffect, LayerDepth);

            return 1;
        }

        #region 事件相关处理程序

        /// <summary>
        /// 重新绘制竖直滚动条
        /// </summary>
        protected override void Redraw()
        {
            //按钮高(宽)度即滚动条宽度
            buttonWidth = size.X;
            //重新设置竖直轨道位置和大小
            sliderRailPosition = new Vector2(Position.X + buttonWidth, Position.Y+ buttonWidth);
            sliderRailSizeX = size.Y - 2 * buttonWidth;
            
            //重新设置滑块的位置和大小
            ResetSliderButtonPosition();
            //滑块宽度由largeChange，sliderRailSizeX和maxStep计算得出
            this.sliderButtonSizeX = largeChange * sliderRailSizeX / (largeChange + maxStep);

            //重新设置向上向下按钮的位置和大小
            btnLeftUp.Position = btnLeftUpPositon = Position;
            btnRightDown.Position = btnRightDownPosition =Position + new Vector2(0, size.Y-buttonWidth);
            btnLeftUp.Size = btnRightDown.Size = new Vector2 (buttonWidth,buttonWidth);            
        }

        /// <summary>
        /// 根据value值重置按钮的位置
        /// </summary>
        protected override void ResetSliderButtonPosition()
        {
            if (!isScrolling)
            {
                //根据value值重置按钮的位置
                sliderButtonPosition.Y = Position.Y+buttonWidth + value * (sliderRailSizeX- sliderButtonSizeX) / maxStep ;
                sliderButtonPosition.X = Position.X+buttonWidth;
                //不让滑块超出滑动条边界
                if (sliderButtonPosition.Y < Position.Y + buttonWidth)
                    sliderButtonPosition.Y = Position.Y + buttonWidth;
                else if (sliderButtonPosition.Y > Position.Y + buttonWidth + sliderRailSizeX - sliderButtonSizeX)
                    sliderButtonPosition.Y = Position.Y + buttonWidth + sliderRailSizeX - sliderButtonSizeX;
            }
        }
        #endregion
    }
}